package com.example.springbootshiro.realm;



import com.example.springbootshiro.pojo.Perms;
import com.example.springbootshiro.pojo.RolePerms;
import com.example.springbootshiro.pojo.User;
import com.example.springbootshiro.pojo.UserRole;
import com.example.springbootshiro.service.IPermsService;
import com.example.springbootshiro.service.IRolePermsService;
import com.example.springbootshiro.service.IUserRoleService;
import com.example.springbootshiro.service.IUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * <p>
 *
 * </p>
 *
 * @author gqz
 * @version 1.0
 * @since 2021/9/2:16:05
 */
public class UserRealm extends AuthorizingRealm {
    @Autowired
    private IUserService userService;
    @Autowired
    private IUserRoleService userRoleService;
    @Autowired
    private IRolePermsService rolePermsService;
    @Autowired
    private IPermsService permsService;
    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行了授权");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //取出user
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        List<Long> roleList = userRoleService.findRoleByUserId(user.getId()).stream().map(UserRole::getRoleId).collect(Collectors.toList());
        Set<String> set=new HashSet<>();
        roleList.forEach(
                id->{
                    List<RolePerms> rolePerms= rolePermsService.findPermsByRoleId(id);
                    List<Long> permsList = rolePerms.stream().map(RolePerms::getPermsId).collect(Collectors.toList());
                    permsList.forEach(
                          p->{
                                  Perms perms =permsService.findPermsById(p);
                                  set.add(perms.getOperate());
                          }
                  );
                }
        );
        //设置权限
        info.addStringPermissions(set);
        return info;
    }
    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("执行了认证");
        //用户名   密码   从数据库中获取
//        String username="root";
//        String password="123456";
       UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
       User user=userService.findUserByUserName(token.getUsername());
        if (user==null)
        {
            //抛出UnknownAccountException
            return null;
        }
        //可以做MD5加密  也可以做MD5盐值加密
        //密码认证,已经进行加密
        //将user对象进行存储
        return new SimpleAuthenticationInfo(user,user.getPassword(),"");
    }
}
